package com.zh.api.controller.complaint;

import com.alibaba.fastjson.JSONObject;
import com.zh.common.utils.CheckEmptyUtil;
import com.zh.common.utils.ResultEnum;
import com.zh.common.utils.ResultVO;
import com.zh.complaint.domain.AdviceDO;
import com.zh.complaint.service.AdviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @ProjectName: healthCare-parent
 * @Package: com.zh.api.controller.third
 * @ClassName: ComplaintController
 * @Description: java类作用描述
 * @Author: lds
 * @CreateDate: 2019/6/3 0003 14:25
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/6/3 0003 14:25
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@RestController
@RequestMapping("/complaint")
@Api("投诉监督")
public class ComplaintController {

    @Autowired
    private AdviceService adviceService;

    /**
     * @param []
     * @return com.chzu.common.base.Result
     * @Author: lds
     * @Description: 投诉接口
     * @Date: Created in 20:57 2019/4/7 0007
     */
    @RequestMapping(value = "saveComplaint", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("投诉接口")
    public ResultVO saveComplaint(@RequestBody AdviceDO adviceDO) {
        if (adviceDO == null) {
            return ResultVO.error(ResultEnum.PARAME_ERROR);
        }

        if (CheckEmptyUtil.isEmpty(adviceDO.getMsgContent())) {
            return ResultVO.error(ResultEnum.PARAME_ERROR);
        }

        if (adviceService.save(adviceDO) > 0) {
            return ResultVO.success();
        }
        return ResultVO.error(ResultEnum.FAILURE);
    }

    /**
     * @param []
     * @return com.chzu.common.base.Result
     * @Author: lds
     * @Description: 投诉列表
     * @Date: Created in 20:59 2019/4/7 0007
     */
    @RequestMapping(value = "list", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("投诉列表")
    public ResultVO getComplaintList(@RequestBody Map<String, Object> param) {
        Map map = new HashMap();
        if (CheckEmptyUtil.isEmpty(param)) {
            return ResultVO.error(ResultEnum.PARAME_ERROR);
        }

        if (Objects.isNull(param.get("msgType"))) {
            return ResultVO.error(ResultEnum.PARAME_ERROR);
        }
        List<AdviceDO> list = adviceService.list(param);
        int total = adviceService.count(param);
        map.put("data", list);
        map.put("total", total);
        return ResultVO.success(map);
    }


    /**
     * 修改
     */
    @RequestMapping(value = "update", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("修改投诉")
    public ResultVO update(@RequestBody AdviceDO adviceDO){
        if(adviceDO==null||CheckEmptyUtil.isEmptys(adviceDO.getId(),adviceDO.getMsgContent())){
            return ResultVO.error(ResultEnum.PARAME_ERROR);
        }


        if(adviceService.update(adviceDO)>0){
            return ResultVO.success();
        }
        return ResultVO.error();
    }

    /**
     * 删除
     */
    @RequestMapping(value = "remove", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("删除投诉")
    public ResultVO remove(@RequestBody AdviceDO adviceDO){

        if(adviceDO==null||CheckEmptyUtil.isEmpty(adviceDO.getId())){
            return ResultVO.error(ResultEnum.PARAME_ERROR);
        }


        if(adviceService.remove(adviceDO.getId())>0){
            return ResultVO.success();
        }
        return ResultVO.error();
    }

    /**
     * 删除
     */
    @RequestMapping(value = "batchRemove", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("批量删除投诉")
    public ResultVO batchRemove(@RequestBody JSONObject jsonParam){
        if(jsonParam==null){
            return ResultVO.error(ResultEnum.PARAME_ERROR);
        }


        List<String> ids= (List<String>) jsonParam.get("ids");

        if(CheckEmptyUtil.isEmpty(ids)){
            return ResultVO.error(ResultEnum.PARAME_ERROR);
        }

        String[] strs1=ids.toArray(new String[ids.size()]);

        if(adviceService.batchRemove(strs1)>0){
            return ResultVO.success();
        }
        return ResultVO.error();
    }

}
